package com.hhh.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.Mapper.AddressbookMapper;
import com.hhh.Pojo.AddressBook;
import com.hhh.Service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressbookSerivce implements AddressBookService {
    @Autowired
    AddressbookMapper addressbookMapper;

    //    查询地址
    @Override
    public List<AddressBook> SelectList(long userid) {
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userid);
        return addressbookMapper.selectList(wrapper);
    }

    //    设置默认地址
    @Override
    @Transactional
    public Integer IsDefault(AddressBook addressBook) {
        int num = 0;
        AddressBook tempaddressBook = new AddressBook();
        tempaddressBook.setIsDefault(0);
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("is_default", 1);
        addressBook.setIsDefault(1);
        num += addressbookMapper.update(tempaddressBook, wrapper);
        num += addressbookMapper.updateById(addressBook);
        if (num == 2)
            return num;
        else
            return 0;
    }

    //    添加地址
    @Override
    public Integer Insert(AddressBook addressBook) {
        return addressbookMapper.insert(addressBook);
    }

    //    查询地址
    @Override
    public AddressBook SelectById(long id) {
        return addressbookMapper.selectById(id);
    }

    //    修改地址
    @Override
    public Integer UpDate(AddressBook addressBook) {
        return addressbookMapper.updateById(addressBook);
    }

    //    删除地址
    @Override
    public Integer Delete(Long ids) {
        return addressbookMapper.deleteById(ids);
    }

    //    获取默认地址
    @Override
    public AddressBook GetDefaultAddress() {
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("is_default", 1);
        return addressbookMapper.selectOne(wrapper);
    }
}
